<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Order;
use App\Services\OrderService;
use Illuminate\Http\Request;

class OrderController extends BaseController
{

    public function index(Request $request)
    {
        $user_id = $this->getUserId();
        $list = Order::where('product_type', Order::PRODUCT_TYPE_COOKBOOK)->where('user_id', $user_id)->where('status', Order::PAID_SUCCESS)
            ->withCertain('cookbook', ['title', 'short_title', 'video_url', 'short_video_url','type'])
            ->withCertain('merchant', ['contact'])->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

    public function createPay(Request $request) {
        $this->validate($request, [
            'entity_id' => 'required',
            'product_type' => 'required|in:1,2,3',
        ]);
        $input = $request->input();
        $user = $this->getUser();
        $user_id = $user->id;
        $input['pay_type'] = 0;
        try {
            $response = OrderService::create_pay($user_id, $input['entity_id'], $input['pay_type'], $input['product_type'], $user->benefit_user);
            return $this->success($response);
        }catch (\Exception $e) {
            \Log::error('create-pay ' . $e->getMessage());
            return $this->failed($e->getMessage());
        }

    }
}

